{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第4章 Python中表示“组”的概念与定义\n",
    "\n",
    "> 列表、元祖都是序列的一种"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-1 列表的定义 list\n",
    "\n",
    "现实世界中总是存在一组一组的事物。在Python中表示组的方法有很多种，并非只有一种  \n",
    "\n",
    "第一种方式：列表。定义一个列表的基本方式：\n",
    "\n",
    "```python\n",
    "type([1,2,3,4,5,6]) # <class 'list'>\n",
    "```\n",
    "\n",
    "列表所共有的特性：对于一个列表的内部的元素的类型，并不一定是某种固定的类型。\n",
    "\n",
    "布尔类型也可以是列表：\n",
    "\n",
    "```python\n",
    "type(['hello','world',1,9,True,False]) # <class 'list'>\n",
    "```\n",
    "\n",
    "甚至列表内的元素也可以是列表, 叫嵌套列表,其他语言叫二维数组：\n",
    "\n",
    "```python\n",
    "type([[1,2],[3,4],[True,False]]) # <class 'list'>\n",
    "```\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "world\n",
      "[234, 54, True]\n"
     ]
    }
   ],
   "source": [
    "list = ['hello', 'world', [234, 54, True]]\n",
    "\n",
    "for tmp in list:\n",
    "    print(tmp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-2 列表的基本操作\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = [1, 2, 3 ,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0:1] # 取范围得到的也是一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[-1] # 取最后一个位置的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 'a', 'b']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr +['a', 'b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-0992cd1578b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marr\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0marr\u001b[0m \u001b[0;31m# 列表之间不能相乘\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'list'"
     ]
    }
   ],
   "source": [
    "arr * arr # 列表之间不能相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'list' and 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-55e709fe39e7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marr\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# 列表之间不能相减\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'list' and 'list'"
     ]
    }
   ],
   "source": [
    "arr - [1, 2] # 列表之间不能相减"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-3 元祖 tuple\n",
    "\n",
    "### 元组tuple和列表list的区别：\n",
    "\n",
    "+ list用`[]`,tuple用`()`\n",
    "+ list 是一种有序集合，可以随时添加和删除其中的元素,用insert()可插入智位置 pop()删除位置\n",
    "+ tuple是一种有序列表，它和list非常相似，但是tuple一旦初始化就不能修改，而且没有append()、insert()这些方法，可以获取元素但不能赋值变成另外的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元祖的声明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n",
      "(1, -1, 'hello', 'world', True, False)\n",
      "<class 'tuple'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "a = (1, 2, 3, 4, 5)\n",
    "b = (1, -1, 'hello', 'world', True, False)\n",
    "print(a)\n",
    "print(b)\n",
    "print(type(a))\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元祖元素的访问(和列表以及字符串的访问类似)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:1] # 取范围得到的还是元祖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'tuple'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-ac686954dc0d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'tuple'"
     ]
    }
   ],
   "source": [
    "a*a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a*2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元祖只有一个元素时的奇怪现象和解释\n",
    "\n",
    "当只有一个元素时会出现奇怪的现象\n",
    "\n",
    "```python\n",
    "type((1)) ->int\n",
    "type(('1')) ->str\n",
    "```\n",
    "\n",
    "():还表示运算优先级，这里就与元组相冲突，python中规定如果括号内只有一个元素，就人为是数学运算，不会认为是元组。\n",
    "\n",
    "正确定义一个元素的元组：(1, )  \n",
    "\n",
    "```python\n",
    "type((1,)) ->tuple\n",
    "```\n",
    "\n",
    "表示空元组的方法：\n",
    "\n",
    "```python\n",
    "type(()) ->()空元组\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 当只有一个元素时，元祖会自动转换成基本数据类型\n",
    "tumple1 = (1)\n",
    "tumple2 = ('hello')\n",
    "print(type(tumple1))\n",
    "print(type(tumple2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 如何定义只有一个元素的元祖呢？在声明的最后加个逗号即可\n",
    "tumple1 = (1,)\n",
    "tumple2 = ('hello',)\n",
    "print(type(tumple1))\n",
    "print(type(tumple2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-4 序列总结 : str、list、tuple共有操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Number: int, float, bool,  \n",
    "序列(组)：str, list, tuple 都是序列。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 序列共有的操作\n",
    "\n",
    "+ 1.取出序列元素(切片)：`[n]`,`[n:m]`、`[:m]`、`[n:]`、`[n,m]`等,取元素时区间都是**左闭右开**地\n",
    "\n",
    "  ```python\n",
    "  [1,2,3,4][0:3] ->[1, 2, 3]\n",
    " 'hello world'[0:8:2] -->'hlow',三个数字的切片\n",
    "  ```\n",
    "\n",
    "+ 2.加法`+`和乘法`*`：运算\n",
    "+ 3.是否包含某个元素： in, not in：\n",
    "  \n",
    "  ```python\n",
    "  3 in [1,2,3] --> True\n",
    "  3 not in [1,2,3] --> False\n",
    "  ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 操作序列的函数  len, max, min, ord\n",
    "\n",
    "```python\n",
    "len([1,2,3]) ->3\n",
    "max([1,2,3,4]) ->4\n",
    "min([1,2,3,4]) ->1\n",
    "max('hello world') ->w : 取ascii码的最大值\n",
    "min('hello world') -> ' ': 输出空格\n",
    "min('helloworld')-> d\n",
    "ord():去ascii码函数：\n",
    "ord('w') ->119\n",
    "ord(' ') ->32\n",
    "ord('d') ->100\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_tmp = ['h', 'e', 'l', 'l', 'o'] # 列表\n",
    "tumple_tmp = ('h', 'e', 'l', 'l', 'o') # 元祖\n",
    "str_tmp = 'hello' # 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去除元素的操作(切片)\n",
      "1. [n]操作\n",
      "h\n",
      "h\n",
      "h\n",
      "o\n",
      "o\n",
      "o\n",
      "2.[n:m]操作\n",
      "['h']\n",
      "('h',)\n",
      "h\n",
      "['h', 'e']\n",
      "('h', 'e')\n",
      "he\n",
      "['h', 'e', 'l', 'l']\n",
      "('h', 'e', 'l', 'l')\n",
      "hell\n",
      "3.[n:]操作\n",
      "['e', 'l', 'l', 'o']\n",
      "('e', 'l', 'l', 'o')\n",
      "ello\n",
      "['l', 'o']\n",
      "('l', 'o')\n",
      "lo\n",
      "4.[:m]操作\n",
      "['h']\n",
      "('h',)\n",
      "h\n",
      "['h', 'e', 'l']\n",
      "('h', 'e', 'l')\n",
      "hel\n"
     ]
    }
   ],
   "source": [
    "print('去除元素的操作(切片)')\n",
    "\n",
    "print(\"1. [n]操作\")\n",
    "print(list_tmp[0])\n",
    "print(tumple_tmp[0])\n",
    "print(str_tmp[0])\n",
    "print(list_tmp[-1])\n",
    "print(tumple_tmp[-1])\n",
    "print(str_tmp[-1])\n",
    "\n",
    "print(\"2.[n:m]操作\")\n",
    "print(list_tmp[0:1])\n",
    "print(tumple_tmp[0:1])\n",
    "print(str_tmp[0:1])\n",
    "print(list_tmp[0:2])\n",
    "print(tumple_tmp[0:2])\n",
    "print(str_tmp[0:2])\n",
    "print(list_tmp[0:-1])\n",
    "print(tumple_tmp[0:-1])\n",
    "print(str_tmp[0:-1])\n",
    "\n",
    "print(\"3.[n:]操作\")\n",
    "print(list_tmp[1:])\n",
    "print(tumple_tmp[1:])\n",
    "print(str_tmp[1:])\n",
    "print(list_tmp[-2:])\n",
    "print(tumple_tmp[-2:])\n",
    "print(str_tmp[-2:])\n",
    "\n",
    "print(\"4.[:m]操作\")\n",
    "print(list_tmp[:1])\n",
    "print(tumple_tmp[:1])\n",
    "print(str_tmp[:1])\n",
    "print(list_tmp[:-2])\n",
    "print(tumple_tmp[:-2])\n",
    "print(str_tmp[:-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(list_tmp) # 长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'o'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(tumple_tmp) # 最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(str_tmp) # 最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "97"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord('a') # 获取字符对应的ASCII码值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a' in list_tmp # 字符是否在字符串中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'o' in tumple_tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'l' not in str_tmp # 字符是否不在字符串中"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-5 set集合\n",
    "\n",
    "集合set用`{}` ps:列表list是`[]`,元祖tuple是`()`\n",
    "\n",
    "+ 1.无序，所以不支持序号、切片\n",
    "+ 2.元素不重复\n",
    "+ 3.`len`\n",
    "+ 4.`1 in {1,2,3}`\n",
    "+ 5.差集 `{1,2,3,4,5,6}-{3,4}  = {1,2,5,6}`\n",
    "+ 6.交集 `{1,2,3}&{3,4} = {3}`\n",
    "+ 7.并集 `{1,2,3,4,5,6,} | {3,4,7} = {1,2,3,4,5,6,7}`\n",
    "+ 8.定义空集合：`set()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_tmp = {1, 2, 3, 4, 5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'set' object does not support indexing",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-32-44416e52bb4e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset_tmp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# 集合不支持序号操作\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing"
     ]
    }
   ],
   "source": [
    "set_tmp[2] # 集合不支持序号操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'set' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-34-57e6aad4626d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset_tmp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# 集合不支持切片操作\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "set_tmp[1:3] # 集合不支持切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{1, 1, 2, 2, 3, 4} # set中不允许有重复元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(set_tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_a = {1, 2, 4, 5, 6, 7}\n",
    "set_b = {1, 2, 3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{4, 5, 6, 7}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_a - set_b # 差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_a & set_b # 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_a | set_b # 并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type({}) # 空set不能这么定义,{}是空dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(set()) # 空set的正确定义方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-6 dict字典\n",
    "\n",
    "dict也是集合的一种，不同地是dict中的元素都是键值对\n",
    "\n",
    "+ 一个字典可以由很多key和value，属于集合类型(set)，不属于序列list和tuple\n",
    "+ 用花括号定义 `{key1:value1, key2:value2...}` \n",
    "+ 字典通过key来访问 `value => { }[key]`\n",
    "+ 字典的key不能重复，重复不会报错，但是会覆盖之前重复的key对应的value\n",
    "+ 字典的key必须是不可变的类型：int、str、元组tuple（列表list、集合set不可以）\n",
    "+ 字典的value可以是任意类型，没有特别限制：str、int、float、list、set、dict等\n",
    "+ 空的字典{ }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_tmp = {'name':'梁山广', 'age': 27, 'company':'Huawei', 86:17601324488}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17601324488"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_tmp[86]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_tmp['age']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-7 思维导图总结基本数据类型\n",
    "\n",
    "+ Python基本数据类型\n",
    "  + 数字Number\n",
    "    + 整型int\n",
    "    + 浮点型float\n",
    "    + 布尔型bool\n",
    "    + 复数complex\n",
    "  + 组\n",
    "    + 序列 \n",
    "      > 均支持下标索引访问，支持切片操作\n",
    "    \n",
    "      + 字符串str，不可变 \n",
    "        + 单引号'' \n",
    "        + 双引号\"\"\n",
    "        + 三引号''''''\n",
    "      + 列表list `[]`\n",
    "      + 元祖tuple `()`\n",
    "    + 集合  \n",
    "      > 均用`{}`\n",
    "    \n",
    "      + set 无序，没有索引，不能切片\n",
    "      + dict 属于set的一种，但是元素都是key/value键值对\n",
    "\n",
    "![基本数据类型](https://img.mukewang.com/szimg/5ca617e200015aa919201080.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
